import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Req } from '@nestjs/common';
import { MemberService } from './member.service';
import { ReqAttrs } from 'apps/admin/src/constant';
import { ResponseUtilService } from '@app/response-util';
import { DtoPipe } from 'apps/admin/src/pipe/dto/dto.pipe';
import PageDto from './dto/page.dto';
import CreateDto from './dto/create.dto';
import UpdateDto from './dto/update.dto';
import ResetPwdDto from './dto/resetpwd.dto';
import ChangePassword from './dto/changePassword.dto';
import UpdateDtoInfo from './dto/UpdateDtoInfo.dto';

@Controller('member')
export class MemberController {
    constructor(
        private readonly service:MemberService
    ) {}

    @Get("permission/menu")
    async permissionMenu(@Req() req) {
        const memberId = req[ReqAttrs.MEMBER_ID];
        const result = await this.service.allMenu(memberId)
        return ResponseUtilService.successRes(result)
    }

    @Post('page')
    async page(@Body(new DtoPipe({skipMissingProperties : true})) dto:PageDto, @Req() req) {
        const result = await this.service.page(dto, req[ReqAttrs.MERCHANT_ID], req[ReqAttrs.MEMBER_ID])

        return ResponseUtilService.successRes(result)

    }

    @Post('add')
    async add(@Body(new DtoPipe()) dto:CreateDto, @Req() req) {
        try {
            const r = await this.service.add(dto, req[ReqAttrs.MERCHANT_ID], req[ReqAttrs.BELONG], req[ReqAttrs.MEMBER_ID])
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Put('update')
    async update(@Body(new DtoPipe()) dto:UpdateDto, @Req() req) {
        const memberId = req[ReqAttrs.MEMBER_ID]
        try {
            const r = await this.service.update(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Delete('delete/:id')
    async del(@Param('id', ParseIntPipe) id:number, @Req() req) {
        if(id < 1) return null
        const memberId = req[ReqAttrs.MEMBER_ID]
        try {
            const r = await this.service.del(id)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }


    @Put('resetpwd')
    async resetpwd(@Body(new DtoPipe()) dto:ResetPwdDto) {
        try {
            const r = await this.service.resetPwd(dto.id, dto.password)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }


    @Put('changePwd')
    async changePwd(@Body(new DtoPipe()) dto:ChangePassword, @Req() req) {
        try {
            const r = await this.service.changePwd(req[ReqAttrs.MEMBER_ID], dto.oldpassword, dto.password)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Put('updateInfo')
    async updateInfo(@Body(new DtoPipe({skipMissingProperties : true})) dto:UpdateDtoInfo, @Req() req) {
        try {
            const r = await this.service.updateInfo(req[ReqAttrs.MEMBER_ID], dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }
}
